package cn.tedu.fresheveryday.mapper;

import cn.tedu.fresheveryday.pojo.entity.ViewHistory;
import cn.tedu.fresheveryday.pojo.vo.ProductViewHistoryDetailVO;
import cn.tedu.fresheveryday.pojo.vo.ViewHistorySimpleVO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface ViewHistoryMapper {
    /**
     * 添加浏览历史
     * @param history
     * @return
     */
    @Insert(
            "insert into view_history (" +
                "id, product_id, user_id, view_time" +
            ") values (" +
                "#{id}, #{productId}, #{userId}, #{viewTime}" +
            ")"
    )
    void insert(ViewHistory history);

    /**
     * 通过用户id查询浏览历史
     * @param userId 用户id
     * @return
     */
    @Select(
            "select id, product_id from view_history " +
            "where user_id = #{userId} " +
            "order by view_time desc " +
            "limit 0, 5"
    )
    @Result(
            column = "product_id", property = "product",
            javaType = ProductViewHistoryDetailVO.class,
            one = @One(select = "cn.tedu.fresheveryday.mapper.ProductMapper.selectDetailById")
    )
    List<ViewHistorySimpleVO> selectByUserId(String userId);

    /**
     * 通过商品id查询浏览历史
     * @param productId 商品id
     */
    @Select("select * from view_history where user_id = #{userId} and product_id = #{productId}")
    @Result(column = "product_id", property = "productId")
//    @Result(column = "user_id", property = "userId")
//    @Result(column = "view_time", property = "viewTime")
    List<ViewHistory> selectByUserIdAndProductId(String userId, int productId);

    /**
     * 通过id修改浏览时间
     * @param id
     * @param viewTime 浏览时间
     */
    @Update("update view_history set view_time = #{viewTime} where id = #{id}")
    void updateById(@Param("id") String id, @Param("viewTime") LocalDateTime viewTime);
}